package top.jfunc.common.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * 一个方便操作map数据的类
 * @param <K>
 * @param <V>
 */
public class DataContainer<K,V>{
    final Map<K,V> map;

    public DataContainer() {
        map = new HashMap<>(4);
    }

    /**
     * 只包含一个数据点额快捷方法
     */
    public DataContainer(K key, V value){
        map = new HashMap<>(4);
        map.put(key,value);
    }
    @SuppressWarnings("unchecked")
    public DataContainer(K key, V value, Object... datas){
        if (datas.length % 2 != 0) {
            throw new IllegalArgumentException("参数错误，必须是2的倍数个参数");
        }
        map = new HashMap<>(1+datas.length/2);
        map.put(key,value);

        for (int i = 0; i < datas.length/2; i+=2) {
            map.put((K)datas[i], (V)datas[i+1]);
        }
    }

    public DataContainer<K,V> by(K key,V value){
        map.put(key,value);
        return this;
    }

    public Map<K,V> toMap(){
        return map;
    }

    public static <KK,VV> DataContainer<KK,VV> of(){
        return new DataContainer<>();
    }
    public static <KK,VV> DataContainer<KK,VV> of(KK kk,VV vv){
        return new DataContainer<>(kk,vv);
    }
    public static <KK,VV> DataContainer<KK,VV> of(KK key,VV value, Object... datas){
        return new DataContainer<>(key,value,datas);
    }
}
